//
//  AddendumTableViewController.m
//  Baby
//
//  Created by YC on 2017/8/8.
//  Copyright © 2017年 YC. All rights reserved.
//

#import "AddendumTableViewController.h"

#import "HomeSection+CoreDataClass.h"
#import "HomeList+CoreDataClass.h"

#import <UITextView+Placeholder/UITextView+Placeholder.h>

@interface AddendumTableViewController () {
    UIDatePicker    *_datePicker;
    UIDatePicker    *_timePicker;
}

@end

@implementation AddendumTableViewController

+ (instancetype)getInstance {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle:nil];
    id vc = [storyboard instantiateViewControllerWithIdentifier:@"AddendumTableViewController"];
    
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self initialization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setupUI {
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.remarkTextView.placeholder = @"填写备注";
    self.remarkTextView.layer.borderWidth = 0.5;
    self.remarkTextView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.remarkTextView.layer.cornerRadius = 5;

}

- (void)initialization {
    NSDate *nowDate = [NSDate date];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    
    _datePicker = [UIDatePicker new];
    [_datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
    [_datePicker setDate:nowDate];
    [_datePicker setLocale:locale];
    _datePicker.maximumDate = nowDate;
    
    [self.dateTextField setInputView:_datePicker];
}

- (IBAction)dateEditingDidEnd:(UITextField *)sender {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString* dateString = [dateFormatter stringFromDate:_datePicker.date];
    
    sender.text = dateString;
}

- (IBAction)clickSaveButton:(id)sender {
    
    HomeList *list = [HomeList MR_createEntity];
    list.time = _datePicker.date;
    list.times = [self.timesTextField.text intValue];
    list.remark = self.remarkTextView.text;
    
    BOOL isHaveSameDay = false;
    
    for (HomeSection *section in self.sections) {
        isHaveSameDay = [Tools whetherTheSameDay:section.date date2:_datePicker.date];
        
        if (isHaveSameDay) {
            NSMutableOrderedSet *set = [section mutableOrderedSetValueForKey:@"list"];
            
            for (int i = 0; i < set.count; i++) {
                HomeList *l = [set objectAtIndex:i];
                
                NSTimeInterval t = [l.time timeIntervalSinceDate:list.time];
                
                if (t < 0) {
                    [set insertObject:list atIndex:i];
                    break;
                }else if (i == set.count - 1) {
                    [set addObject:list];
                }
            }
            
            break;
        }
    }
    
    if (isHaveSameDay == false) {
        HomeSection *section = [HomeSection MR_createEntity];
        
        section.date = _datePicker.date;
        
        NSMutableOrderedSet *set = [section mutableOrderedSetValueForKey:@"list"];
        [set insertObject:list atIndex:0];
    }
    
    if (self.doneBlock != nil) {
        self.doneBlock();
    }

    [self.navigationController popViewControllerAnimated:true];
}


@end
